﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using FourthWallProject.GameObjects.Solids;

namespace FourthWallProject.GameObjects.GameComponents
{
    class LevelGrid : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public StaticGameTile[,] Grid
        {
            get;
            private set;
        }

        float TileWidth;
        float TileHeight;

        public LevelGrid(int XLength, int YLength, Game game)
            : base(game)
        {
            Grid = new StaticGameTile[XLength, YLength];

            for (int y = 0; y < Grid.GetLength(1); y++)
            {
                for (int x = 0; x < Grid.GetLength(0); x++)
                {
                    Grid[x, y] = null;
                }
            }

            TileWidth = 15;
            TileHeight = 15;
        }

        public void AddTile(int x, int y, StaticGameTile Tile)
        {
            if (Tile != null)
            {
                Tile.X = x * TileWidth;
                Tile.Y = y * TileHeight;
                Tile.Width = (int)TileWidth;
                Tile.Height = (int)TileHeight;
                Grid[x, y] = Tile;
            }
        }

        public void RemoveTile(int x, int y)
        {
            Grid[x, y] = null;
        }

        public bool CheckCollision(PhysicsEnabledGameObject gameobject)
        {
            //bool intersect = false;
            //for (int y = 0; y < Grid.GetLength(1); y++)
            //{
            //    for (int x = 0; x < Grid.GetLength(0); x++)
            //    {
            //        if (Grid[x, y] != null)
            //        {
            //            if (Grid[x, y].SolidState == SolidState.Solid)
            //            {
            //                if (gameobject.Rectangle.Intersects(Grid[x, y].Rectangle))
            //                {
            //                    Grid[x, y].IsIntersected = true;
            //                    return true;
            //                }
            //                else
            //                {
            //                    Grid[x, y].IsIntersected = false;
            //                }
            //            }
            //        }
            //    }
            //}
            //return intersect;

            //The Char

            List<Vector2> gameobjectsgridcells = new List<Vector2>();

            for (int xc = 0; xc < gameobject.Width; xc++)
            {
                for (int yc = 0; yc < gameobject.Height; yc++)
                {
                    var x = gameobject.X + xc;
                    var y = gameobject.Y + yc;

                    var gridX = (int)Math.Floor(x / TileWidth);
                    var gridY = (int)Math.Floor(y / TileWidth);
                    if (!(gameobjectsgridcells.Where(v => v.X == gridX && v.Y == gridY).Count() > 0))
                        gameobjectsgridcells.Add(new Vector2(gridX, gridY));
                }
            }
            foreach (var vector in gameobjectsgridcells)
            {
                if (vector.X >= 0 && vector.Y >= 0 && vector.X < GridLength && vector.Y < GridHeight)
                {
                    var currentTile = Grid[(int)vector.X, (int)vector.Y];
                    if (currentTile != null)
                    {
                        if (currentTile.SolidState == SolidState.Solid)
                        {
                            if (gameobject.CurrentSpeedX < 0)
                            {
                                gameobject.X = currentTile.Rectangle.Right + 1;
                            }
                            else if (gameobject.CurrentSpeedX > 0)
                            {
                                gameobject.X = currentTile.Rectangle.Left - gameobject.Width - 1;
                            }
                            if (gameobject.CurrentSpeedY < 0)
                            {
                                gameobject.Y = currentTile.Rectangle.Bottom + 1;
                            }
                            else if (gameobject.CurrentSpeedY > 0)
                            {
                                gameobject.Y = currentTile.Rectangle.Top - gameobject.Height - 1;
                            }

                            return true;

                        }
                        else
                            if (currentTile.SolidState == SolidState.End)
                                GameState.LevelScreen.NextLevel();
                    }
                }
                else
                    return true;
            }

            return false;

            //Bottom-Right

            //var x = gameobject.Rectangle.X + gameobject.Rectangle.Width;
            //var y = gameobject.Rectangle.Y + gameobject.Rectangle.Height;

            //var gridX = (int)Math.Floor(x / TileWidth);
            //var gridY = (int)Math.Floor(y / TileWidth);
            //if (gridX >= 0 && gridY >= 0 && gridX < GridLength && gridY < GridHeight)
            //{
            //    if (Grid[gridX, gridY] != null)
            //    {
            //        gameobject.X = Grid[gridX, gridY].Rectangle.Left - gameobject.Width - 1;
            //        gameobject.Y = Grid[gridX, gridY].Rectangle.Top - gameobject.Height - 1;
            //        return true;
            //    }
            //}
            //else
            //    return true;
            ////BottomLeft
            //x = gameobject.Rectangle.X;
            //y = gameobject.Rectangle.Y + gameobject.Rectangle.Height;

            //gridX = (int)Math.Floor(x / TileWidth);
            //gridY = (int)Math.Floor(y / TileWidth);
            //if (gridX >= 0 && gridY >= 0 && gridX < GridLength && gridY < GridHeight)
            //{
            //    if (Grid[gridX, gridY] != null)
            //    {
            //        gameobject.X = Grid[gridX, gridY].Rectangle.Right - 1;
            //        gameobject.Y = Grid[gridX, gridY].Rectangle.Top - gameobject.Height - 1;
            //        return true;
            //    }
            //}
            //else
            //    return true;
            ////Top-Left
            //x = gameobject.Rectangle.X;
            //y = gameobject.Rectangle.Y;

            //gridX = (int)Math.Floor(x / TileWidth);
            //gridY = (int)Math.Floor(y / TileWidth);
            //if (gridX >= 0 && gridY >= 0 && gridX < GridLength && gridY < GridHeight)
            //{
            //    if (Grid[gridX, gridY] != null)
            //    {
            //        gameobject.X = Grid[gridX, gridY].Rectangle.Right - 1;
            //        gameobject.Y = Grid[gridX, gridY].Rectangle.Bottom - 1;
            //        return true;
            //    }
            //}
            //else
            //    return true;

            ////Top-Right
            //x = gameobject.Rectangle.X + gameobject.Rectangle.Width;
            //y = gameobject.Rectangle.Y;

            //gridX = (int)Math.Floor(x / TileWidth);
            //gridY = (int)Math.Floor(y / TileWidth);
            //if (gridX >= 0 && gridY >= 0 && gridX < GridLength && gridY < GridHeight)
            //{
            //    if (Grid[gridX, gridY] != null)
            //    {
            //        gameobject.X = Grid[gridX, gridY].Rectangle.Left - gameobject.Width - 1;
            //        gameobject.Y = Grid[gridX, gridY].Rectangle.Bottom - 1;
            //        return true;
            //    }
            //}
            //else
            //    return true;

            //return false;
        }


        public override void Draw(GameTime gameTime)
        {
            ScreenManager.SpriteBatch.Begin();

            for (int y = 0; y < Grid.GetLength(1); y++)
            {
                for (int x = 0; x < Grid.GetLength(0); x++)
                {
                    if (Grid[x, y] != null)
                    {
                        if (Grid[x, y].IsIntersected)
                        {
                            ScreenManager.SpriteBatch.Draw(Grid[x, y].Texture, new Rectangle((int)this.Grid[x, y].Position.X + GameCamera.X, (int)this.Grid[x, y].Position.Y + GameCamera.Y, (int)TileWidth, (int)TileHeight), null, Color.Red);
                        }
                        else
                        {
                            ScreenManager.SpriteBatch.Draw(Grid[x, y].Texture, new Rectangle((int)this.Grid[x, y].Position.X + GameCamera.X, (int)this.Grid[x, y].Position.Y + GameCamera.Y, (int)TileWidth, (int)TileHeight), null, Grid[x, y].Color);
                        }
                    }
                }
            }
            ScreenManager.SpriteBatch.End();

        }

        public int GridLength
        {
            get { return Grid.GetLength(0); }
        }

        public int GridHeight
        {
            get { return Grid.GetLength(1); }
        }

        internal bool CheckCollisionBelow(PhysicsEnabledGameObject gameobject)
        {
            bool intersect = false;
            var x = gameobject.Rectangle.X + (gameobject.Rectangle.Width / 2);
            var y = gameobject.Rectangle.Y + gameobject.Rectangle.Height;

            var gridX = (int)Math.Floor(x / TileWidth);
            var gridY = (int)Math.Floor( y / TileWidth);

            if (gridX >= 0 && gridY >= 0 && gridX < GridLength && gridY < GridHeight)
            {
                if (Grid.GetLength(1) > gridY + 1)
                {
                    if (Grid[gridX, gridY + 1] == null)
                    {
                        return true;
                    }
                }
            }

            return intersect;
        }
    }
}